package com.xianyun.site.iblog.blog.executor;

import com.alibaba.cola.catchlog.CatchAndLog;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.xianyun.common.core.domain.TokenContent;
import com.xianyun.common.security.common.TokenConfig;
import com.xianyun.site.iblog.api.blog.ArticleServiceI;
import com.xianyun.site.iblog.blog.executor.command.ArticleAddCmdExo;
import com.xianyun.site.iblog.blog.executor.query.ArticleListSortQryExo;
import com.xianyun.site.iblog.blog.executor.query.ArticleViewByUrlQryExo;
import com.xianyun.site.iblog.blog.executor.query.ColumnSearchQryExo;
import com.xianyun.site.iblog.blog.executor.query.TagSearchQryExo;
import com.xianyun.site.iblog.dto.blog.*;
import com.xianyun.site.iblog.dto.blog.data.ArticleDTO;
import com.xianyun.site.iblog.co.blog.ArticleCO;
import com.xianyun.site.iblog.dto.blog.data.ColumnDTO;
import com.xianyun.site.iblog.dto.blog.data.TagsDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 文章服务实现接口 服务层
 * @author mac_zyj
 */
@Service
@CatchAndLog
public class ArticleServiceImpl implements ArticleServiceI {
    private static final String MODEL= "文章管理" ;

    @Autowired
    private TokenConfig tokenConfig;

    @Autowired
    private ArticleViewByUrlQryExo articleViewByUrlQryExo;

    @Autowired
    private ArticleListSortQryExo articleListSortQryExo;

    @Autowired
    private ArticleAddCmdExo articleAddCmdExo;

    @Autowired
    private TagSearchQryExo tagSearchQryExo;

    @Autowired
    private ColumnSearchQryExo columnSearchQryExo;

    @Override
    public SingleResponse<ArticleCO> getArticleViewByUrl(ArticleViewByUrlQry qry) {
        if ( null != qry.getToken()){
            TokenContent tokenContent=tokenConfig.checkToken(qry.getToken(), qry.getSessionId(), MODEL,false);
            if (null != tokenContent){
                qry.setUid(tokenContent.getUid());
            }
        }
        return articleViewByUrlQryExo.execute(qry);
    }

    @Override
    public MultiResponse<TagsDTO> selectUserTagList(TagSearchQry qry) {
        return tagSearchQryExo.execute(qry);
    }

    @Override
    public MultiResponse<ColumnDTO> selectUserColumnList(ColumnSearchQry qry) {
        return columnSearchQryExo.execute(qry);
    }

    @Override
    public PageResponse<ArticleDTO> selectArticleListBySort(ArticleListSortQry qry) {
        TokenContent tokenContent=tokenConfig.checkToken(qry.getToken(),qry.getSessionId(),MODEL,true);
        qry.setUid(tokenContent.getUid());
        return articleListSortQryExo.execute(qry);
    }

    @Override
    public Response saveBlogArticle(ArticleAddCmd cmd) {
        TokenContent tokenContent=tokenConfig.checkToken(cmd.getToken(), cmd.getSessionId(), MODEL,true);
        cmd.setUid(tokenContent.getUid());
        return articleAddCmdExo.execute(cmd);
    }

    @Override
    public Response updateBlogArticle(ArticleAddCmd cmd) {
        TokenContent tokenContent=tokenConfig.checkToken(cmd.getToken(), cmd.getSessionId(), MODEL,true);
        cmd.setUid(tokenContent.getUid());
        return articleAddCmdExo.execute(cmd);
    }

    @Override
    public Response deleteBlogArticle(Integer blogId, String token) {
        return null;
    }
}
